import { ApiResponse, User, UserProfile } from "@/types/data";
import { RootThunkAction } from "@/types/store";
import request from "@/utils/request";

export function getUser(): RootThunkAction {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<User>>('/user');
    dispatch({
      type: 'profile/getUser',
      payload: res.data.data
    })
  }
}

export function getUserProfile(): RootThunkAction {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<UserProfile>>('/user/profile')
    dispatch({
      type: 'profile/getUserProfile',
      payload: res.data.data
    })
  }
}

export function updateUserProfile(data: Partial<UserProfile>): RootThunkAction {
  return async (dispatch) => {
    await request.patch('/user/profile', data);
    // 重新获取新的信息
    dispatch(getUserProfile())
  }
}

export function updatePhoto(data: FormData): RootThunkAction {
  return async (dispatch) => {
    await request.patch('/user/photo', data)
    // 重新获取新的信息
    dispatch(getUserProfile())
  }
}